/**
 * Copyright 2019 Inc.
 **/
package com.myz.demo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;

import java.util.List;

/**
 * JacksonEncoder依赖:被用于JSON格式的API
 * <dependency>
 * <groupId>com.netflix.feign</groupId>
 * <artifactId>feign-jackson</artifactId>
 * <version>8.18.0</version>
 * </dependency>
 *
 * @author maoyz0621 on 19-7-11
 * @version: v1.0
 */
public class JacksonFeign {

    /**
     * 使用JacksonEncoder() 和 JacksonDecoder()
     *
     * @param args
     */
    public static void main(String... args) {
        // 设置ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .configure(SerializationFeature.INDENT_OUTPUT, true)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        GitHub github = Feign.builder()
                .encoder(new JacksonEncoder(objectMapper))
                .decoder(new JacksonDecoder(objectMapper))
                .target(GitHub.class, "https://api.github.com");

        List<Contributor> contributors = github.contributors("OpenFeign", "feign");
        for (Contributor contributor : contributors) {
            System.out.println(contributor.login + " (" + contributor.contributions + ")");
        }
    }
}